package com.android.ailaoshan.welcome;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.android.ailaoshan.R;
import com.android.ailaoshan.index.MainActivity;
import com.android.ailaoshan.tools.CheckInternet;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class WelcomeActivity extends AppCompatActivity
        implements EasyPermissions.PermissionCallbacks {

    // 应用所需的所有危险权限
    private final String[] NEEDS_PERMISSIONS = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO
    };

    private final int PERMISSION_CODE = 101;

    private Context mContext;

    private TextView welcomeTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        mContext = WelcomeActivity.this;
        welcomeTxt = findViewById(R.id.welcome_status);
    }

    @Override
    protected void onResume(){
        super.onResume();

        checkPermissions(NEEDS_PERMISSIONS);
    }

    /**
     * 检查权限
     * 请求权限
     */
    private void checkPermissions(String[] perms){

        welcomeTxt.setText(R.string.welcome_permissions);

        if( EasyPermissions.hasPermissions(mContext,perms) ){
            checkInternet();
        }else{
            EasyPermissions.requestPermissions(
                    WelcomeActivity.this,
                    mContext.getString(R.string.permissions_request),
                    PERMISSION_CODE,
                    perms);
        }
    }

    /**
     * 权限请求结果回调，调用EasyPermissions来处理
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    /**
     * 权限请求成功
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        switch (requestCode){
            case PERMISSION_CODE:{
                checkInternet();

                break;
            }
        }
    }

    /**
     * 权限请求失败，退出应用
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        alertMsg(R.string.permission_alert_title, R.string.permission_alert_msg);
    }

    /**
     * 检查网络连接状况
     */
    private void checkInternet(){
        welcomeTxt.setText(R.string.welcome_network);

        CheckInternet checkInternet = new CheckInternet();

        boolean isNetAvailable = checkInternet.isInternetAvailable(mContext);
        if(isNetAvailable){
            // 检查用户名
            // *******
            gotoMapView();
        }else{
            // 网络连接不可用，退出应用
            alertMsg(R.string.network_alert_title, R.string.network_alert_msg);
        }
    }

    /**
     * 显示提示框，退出应用
     */
    private void alertMsg(int titleId,int msgId){
        new AlertDialog.Builder(WelcomeActivity.this)
                .setTitle(titleId)
                .setMessage(msgId)
                .setCancelable(false)
                .setPositiveButton(
                        R.string.permission_alert_btn, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                WelcomeActivity.this.finish();
                            }
                        }).show();
    }

    /**
     * Activity跳转
     */
    private void gotoMapView(){
        Intent intent = new Intent();
        intent.setClass(WelcomeActivity.this, MainActivity.class);
        mContext.startActivity(intent);

        WelcomeActivity.this.finish();
    }
}
